local base = _G
module("GUI.EventHandler.Form.Order_Detail")

local os = base.os.getenv("OS")
if os == "Windows_NT" then
	rootdir = ".\\"
else
	rootdir = "\\Program Files\\wmssystem\\"
end

base.print("DBG:Eventhandler GUI.EventHandler.Form.Order_Detail Loaded.\n")
function Parse(methon,para)
	local utils = base.require("Library.Utils.Utils")
	para = utils.Base64_Decode(para)
	base.print("DBG: methon: " .. methon .. " para: " .. para .. "\n")
	if(methon == "back") then
		back()
	elseif(methon == "SetToComplete") then
		SetToComplete(para)
	elseif(methon == "ReadCard") then
		ReadCard(para)
	else
		base.print("DBG:Unknown action\n")
	end
	base.os.exit(0)
end

function back()
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("main")
	file:close()
end
function SetToComplete(OrderID)
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("main")
	file:close()
	
	local f = base.require("Function.order_management")
	local r = f.SetToComplete(OrderID)
	
	local utils = base.require("Library.Utils.Utils")
	
	if r[1][1] ~= "Succeed" then
		utils.MessageBox("Error while setting order to shipped.\nMessage:\n" .. r[2][1],"Error",1)
	else
		utils.MessageBox("Succeed setted order to shipped.","Succeed",1)
	end
end
function ReadCard(orderid)
	local cc = base.require("Library.COMController.COMController")
	local f = base.require("Function.order_management")
	local utils = base.require("Library.Utils.Utils")
	
	rfid_id = cc.ReadContent(3)
	
	local s_file = base.io.open(rootdir .. "tmp\\order_detail_tmp.txt","r")
	if r_file == nil then
		local content = f.Query(orderid)
		local s_file = base.io.open(rootdir .. "tmp\\order_detail_tmp.txt","w")
		s_file:write(content)
		s_file:close()
	else
		s_file:close()
	end
	
	local s_file = base.io.open(rootdir .. "tmp\\order_detail_tmp.txt","r")
	stock = s_file:read("*all")
	s_file:close()
	if stock == base.string.gsub(stock,rfid_id,"") then
		cc.Beep(3)
	else
		stock = base.string.gsub(stock,rfid_id,"")
		local s_file = base.io.open(rootdir .. "tmp\\order_detail_tmp.txt","w")
		s_file:write(stock)
		s_file:close()
		utils.MessageBox("OK","OK",1)
	end
end